unit DokumentFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBBaseFrm, cxClasses, cxStyles, cxGridBandedTableView, DB,
  DBClient, MConnect, alBaseDataSet, cxEdit, cxContainer, StdActns,
  ActnList, StdCtrls, ExtCtrls, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxControls, cxGridCustomView, cxGrid,
  cxGridDBBandedTableView, cxEditRepositoryItems, cxDropDownEdit, Menus,
  ImgList, PoslovanjeSrv_TLB, cxDataStorage,
  cxExtEditRepositoryItems, kdbinfo, kdbstruc, GlobalSettings, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TcxCustomDropDownInnerEditCrack = class(TcxCustomDropDownInnerEdit);

  TcxCustomDropDownEditCrack = class(TcxCustomDropDownEdit);

  TfrmDokument = class(TfrmDBBase)
    pnlClient: TPanel;
    grdMain: TcxGrid;
    grdlvlMain: TcxGridLevel;
    grdtvMain: TcxGridDBBandedTableView;
    rpstMainStevilka: TcxEditRepositoryMaskItem;
    rpstMainDatum: TcxEditRepositoryDateItem;
    rpstMainOpomba: TcxEditRepositoryTextItem;
    grdtvMainStevilka: TcxGridDBBandedColumn;
    grdtvMainDatum: TcxGridDBBandedColumn;
    grdtvMainOpomba: TcxGridDBBandedColumn;
    grdtvMainID: TcxGridDBBandedColumn;
    grdtvMainRazredID: TcxGridDBBandedColumn;
    grdtvMainRefID: TcxGridDBBandedColumn;
    scnPoslovnoLeto: TSharedConnection;
    cdsPoslovnoLeto: TBaseDataSet;
    dtsrcPoslovnoLeto: TDataSource;
    actnPrestevilcenje: TAction;
    rpstMainStatus: TcxEditRepositoryMaskItem;
    grdtvMainStatus: TcxGridDBBandedColumn;
    rpstMainStatusEdit: TcxEditRepositoryCheckComboBox;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure OnValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
    procedure actnOpravilaExecute(Sender: TObject);
    procedure OnFilterControlDialogShow(Sender: TObject);
    procedure OnEnter(Sender: TObject);
    procedure OnExit(Sender: TObject);
  private
    { Private declarations }
  protected
    function GetEnabledButtons: TDBActionSet; override;
    function GetSessionParamRelations(DataSet: TDataSet): TSessionParamRelations; override;
    procedure Deinitialize; override;
    procedure Initialize; override;
    procedure Loaded; override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure Renumber(StartValue, EndValue: Integer);
  end;

var
  frmDokument: TfrmDokument;

implementation

uses
  MainFrm, DokumentPrestevilcenjeFrm;

{$R *.dfm}

{ TfrmDokument }

procedure TfrmDokument.actnOpravilaExecute(Sender: TObject);
var
  Form: TfrmDokumentPrestevilcenje;
  ModalResult: Integer;
  ZacetnaVrednost: Integer;
  KoncnaVrednost: Integer;
begin
  inherited;
  if Sender = actnPrestevilcenje then
  begin
    Form := TfrmDokumentPrestevilcenje.Create(nil, Session);
    try
      ModalResult := Form.ShowModal;
      if ModalResult = mrOK then
      begin
        ZacetnaVrednost := StrToInt(VarToStrDef(Form.edtZacetnaVrednost.Properties.Value, '0'));
        KoncnaVrednost := StrToInt(VarToStrDef(Form.edtKoncnaVrednost.Properties.Value, '0'));
        Renumber(ZacetnaVrednost, KoncnaVrednost);
        cdsMain.Refresh;
      end;
    finally
      Form.Free;
    end;
  end;
end;

constructor TfrmDokument.Create(AOwner: TComponent);
begin
  inherited;
  with grdtvMain do
  begin
    DataController.Options :=
      DataController.Options + [dcoAssignGroupingValues, dcoAnsiSort];
    NavigatorButtons.ConfirmDelete := False;
    NavigatorButtons.Filter.Visible := False;
    OptionsCustomize.BandMoving := False;
    OptionsCustomize.ColumnFiltering := False;
    OptionsCustomize.ColumnVertSizing := False;
    OptionsCustomize.ColumnMoving := False;
    OptionsCustomize.ColumnSorting := False;
    OptionsData.CancelOnExit := False;
    OptionsData.Deleting := False;
    OptionsData.Editing := False;
    OptionsData.Inserting := False;
    OptionsSelection.CellSelect := False;
    OptionsView.CellEndEllipsis := True;
    OptionsView.ColumnAutoWidth := True;
    OptionsView.GroupByBox := False;
    OptionsView.Indicator := True;
    OptionsView.BandHeaders := False;
    OptionsView.HeaderAutoHeight := True;
    OptionsView.Navigator := True;
    //OptionsView.GroupRowStyle := grsOffice11;
  end;
end;

procedure TfrmDokument.Deinitialize;
begin
  cdsPoslovnoLeto.Close;
  inherited;
end;

procedure TfrmDokument.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    13:
      begin
        if ActiveControl is TcxCustomDropDownInnerEdit then
        begin
          with TcxCustomDropDownEditCrack(TcxCustomDropDownInnerEditCrack(ActiveControl).Container) do
          begin
            if DroppedDown then
            begin
              CloseUp(crEnter);
              //Perform(WM_NEXTDLGCTL, 1, 0);
            end;
          end;
        end;
      end;
  end;
  inherited;
end;

function TfrmDokument.GetEnabledButtons: TDBActionSet;
begin
  Result := inherited GetEnabledButtons;
  if Assigned(RootDataSource) and Assigned(RootDataSource.DataSet) and (RootDataSource.DataSet.Active) then
    with RootDataSource.DataSet as TBaseDataSet do
  begin
    Result := Result + [daFilter];
  end;
end;

function TfrmDokument.GetSessionParamRelations(DataSet: TDataSet): TSessionParamRelations;
begin
  if DataSet = cdsMain then
  begin
    SetLength(Result, 1);
    Result[0].FieldName := 'REF_ID';
    Result[0].SessionName := 'POSLOVNO_LETO_ID';
  end
  else if DataSet = cdsPoslovnoLeto then
  begin
    SetLength(Result, 1);
    Result[0].FieldName := 'REF_ID';
    Result[0].SessionName := 'PODJETJE_ID';
  end;
end;

procedure TfrmDokument.Initialize;
begin
  inherited;
  cdsPoslovnoLeto.Open;
end;

procedure TfrmDokument.Loaded;
var
  i: Integer;
begin
  inherited;
  with grdtvMain do
  begin
    grdtvMainID.Visible := False;
    grdtvMainID.Options.Filtering := False;
    grdtvMainRazredID.Visible := False;
    grdtvMainRazredID.Options.Filtering := False;
    grdtvMainRefID.Visible := False;
    grdtvMainRefID.Options.Filtering := False;
    grdtvMainStatus.Visible := False;
    for i := 0 to ColumnCount - 1 do
    begin
      Columns[i].Position.BandIndex := 0;
      Columns[i].Position.RowIndex := 0;
      Columns[i].Position.ColIndex := i;
    end;
  end;
end;

procedure TfrmDokument.OnValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  DatumZacetek: TDateTime;
  DatumKonec: TDateTime;
  Datum: TDateTime;
begin
  inherited;
  if cdsPoslovnoLeto.Locate('ID', Session.Params['POSLOVNO_LETO_ID'], []) then
  begin
    DatumZacetek := cdsPoslovnoLeto.Fields.FieldByName('DATUM_ZACETEK').AsDateTime;
    DatumKonec := cdsPoslovnoLeto.Fields.FieldByName('DATUM_KONEC').AsDateTime;
    try
      Datum := VarToDateTime(DisplayValue);
      if (Datum < DatumZacetek) or (Datum > DatumKonec) then
      begin
        ErrorText := Format('Vrednost polja Datum mora biti med %s in %s',
          [FormatDateTime('c', DatumZacetek), FormatDateTime('c', DatumKonec)]);
        Error := True;
      end;
    except
      ErrorText := Format('Napaka pri vnosu: %s', [DisplayValue]);
      Error := True;
    end;
  end;
end;

procedure TfrmDokument.Renumber(StartValue, EndValue: Integer);
begin
  (cdsMain.AppServer as IrdmDokument).Renumber(Session, 'STEVILKA', 'DATUM', StartValue, EndValue)
end;

procedure TfrmDokument.OnEnter(Sender: TObject);
begin
  if Sender is TcxGrid then
    TcxGrid(Sender).RootLevelOptions.DetailFrameColor := ActiveControlColor;
end;

procedure TfrmDokument.OnExit(Sender: TObject);
begin
  if Sender is TcxGrid then
    TcxGrid(Sender).RootLevelOptions.DetailFrameColor := InactiveControlColor;
end;

procedure TfrmDokument.OnFilterControlDialogShow(Sender: TObject);
begin
  inherited;
  LocalizeFilter(Sender);
end;

end.
